from selenium import webdriver
import time
import os
import NBVisiter as NBV
import json
from AllVisiter import *
from Searcher import *

MAX_PAGE_NUM = 2
KEYS=["顶替"]

if os.path.exists("settings.json"):
    print("检查到配置文件，将使用配置文件内的内容：")
    settings = json.load(open("settings.json", 'r+', encoding='utf-8'))
    KEYS = settings['keys']
    MAX_PAGE_NUM = settings['max_page_num']
    print("\t", KEYS)
    print('\t',MAX_PAGE_NUM)

if os.path.exists('out_put//temp') is False:
    os.makedirs('out_put//temp')
if os.path.exists('temp') is False:
    os.makedirs('temp')

Out_put_dir = ".\out_put"
errorlink_reerror = []
d = webdriver.Chrome(executable_path='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe')
d.get("http://www.weibo.cn")
d.find_element_by_link_text('登录').click()
input("登陆完成就继续。")

bs = BaiduSearcher(
    Keys=KEYS,
    driver=d,
    Search_AT="m.weibo.cn",
    Out_Keys=["的微博"],
    Out_equal_keys=["微博"],
    max_page_num=MAX_PAGE_NUM
)

visiter = []
visiter.append(
    M_WEIBO_CN_Visiter(
        Name="WEIBOCN",
        CSS_Reply=".m-img-box+div",
        CSS_User=".m-text-box>a>h3.m-text-cut",
        CSS_Text=".weibo-og>.weibo-text",
        CSS_Time="h4.m-text-cut>.time",
        CSS_Fabulous=".tab-item.cur>i+i",
        BrowserDriver=d
    )
)
visiter.append(
    WEIBO_COM_Visiter(
        Name="weibo.com",
        CSS_Text="div.woo-box-item-flex.Txt_cut_1Pb86",
        CSS_User=".star-autocut.star-f16",
        CSS_Reply=".vue-recycle-scroller__item-view",
        CSS_Time=".woo-box-flex.woo-box-alignCenter.woo-box-justifyBetween.Detail_tith4_3_UzS>.star-f16",
        CSS_Fabulous=".woo-box-flex.woo-box-alignCenter.woo-box-justifyBetween.Detail_tith4_3_UzS>.star-f16",
        BrowserDriver=d
    )
)
visiter.append(
    WEIBO_COM_2(
        Name="WEIBOOM_2",
        CSS_Reply=".list_ul>.list_li.S_line1.clearfix>.WB_face.W_fl+div",
        CSS_User="a.W_f14.W_fb.S_txt1",
        CSS_Text=".WB_detail div.WB_text.W_f14",
        CSS_Time=".WB_info+.WB_from.S_txt2",
        CSS_Fabulous=".line.S_line1>span>em.W_ficon.ficon_praised.S_txt2+em",
        BrowserDriver=d
    )
)


def write_to_json(weibo_list, filename="out_put_" + str(time.time()) + ".json"):
    file_name = os.path.join(Out_put_dir, filename)
    print("正在写入：" + file_name)
    with open(file_name, "w+", encoding="utf-8") as f:
        f.write(json.dumps(weibo_list, ensure_ascii=False, indent=4))


def show_time(start_t, p):
    now_t = time.time()
    t = now_t - start_t
    ana = (t / p) * (1 - p)
    print("已用时 %d 秒(%.1f 分钟)" % (t, t / 60))
    print("预计剩余 %d 秒(%.1f 分钟)，\n预计完成时间 %s." % (ana, ana / 60, time.asctime(time.localtime(now_t + ana))))


############################第 1 级搜索#######################################
myin = input("请输入搜索结果json文件.输入-1取消.")
if myin == "-1":
    print("\n\n现在是自动搜索模式.\n\n")
    res = bs.go()
    bs.write_name("temp\RES_" + str(time.time()) + ".txt")
    print("完成了第 1 级搜索。")
    write_to_json(res, "res_1" + str(time.time()) + ".json")
else:
    print("现在将加载：%s"%myin)
    res=json.load(open(myin, 'r+', encoding='utf-8'))
###################################################################
cnt_allteply = 0
############################第 2 级搜索#######################################
out_put_2 = []
errorlink = []
a = len(res)
b = 1
start_t = time.time()
llong = 0
out_put_1 = {}
for mylnk in res:
    time.sleep(2)
    print("已完成%5.2f%%, (%d/%d)" % (b / a * 100, b, a))
    show_time(start_t, b / a)
    link_text = str(d.current_url)
    if link_text.find('?nick=') >= 0:
        errorlink_reerror.append(link_text)  # 直接否决
        print("直接否决一个 %s" % link_text)
        continue
    elif link_text.find('m.weibo.cn') >= 0:
        out_put_1 = visiter[0].go(mylnk)
    elif link_text.find(r'weibo.com/tv') >= 0:
        out_put_1 = visiter[1].go(mylnk)
    elif link_text.find('sg.weibo') >= 0:
        print('暂不支持这种网址。')
        errorlink.append(link_text)
    else:
        out_put_1 = visiter[2].go(mylnk)
    if len(out_put_1) == 0:
        print("看来出了一点问题，他是：")
        print(link_text)
        errorlink.append(link_text)
        """for v in visiter:
            out_put_1 = v.go(mylnk)
            if len(out_put_1) == 0:
                continue
            else:
                out_put_2.append(out_put_1)
                break"""
    else:
        cnt_allteply += out_put_1["reply_num"]
        out_put_2.append(out_put_1)
    b += 1
    llong = len(out_put_2)
    print("当前有效条目：" + str(llong))
    print("有效率：%.2f, 预计有效条目数： %d" % ((llong / b), (llong / b) * a))
    if b % 17 == 0:
        write_to_json(out_put_2, filename="temp\\temp%03d.json" % llong)
        write_to_json(errorlink, filename="temp\\errtemp%03d.json" % llong)
write_to_json(out_put_2, filename="temp\\temp%03d.json" % llong)
print("完成了第 2 级搜索。")
###################################################################

#############################第3级搜索：将错误的部分再走一遍######################################
# errorlink_reerror = []
before_b = b
b = 1
a = len(errorlink)
start_t = time.time()
for mylnk in errorlink:
    time.sleep(2)
    print("重复查错 已完成%6.2f%%, (%d/%d)" % (b / a * 100, b, a))
    show_time(start_t, b / a)
    link_text = str(d.current_url)
    if link_text.find('m.weibo.cn') >= 0:
        out_put_1 = visiter[0].go(mylnk, delay=4)
    elif link_text.find('weibo.com/tv') >= 0:
        out_put_1 = visiter[1].go(mylnk, delay=4)
    elif link_text.find('sg.weibo') >= 0:
        print('暂不支持这种网址。')
        errorlink_reerror.append(link_text)
    else:
        out_put_1 = visiter[2].go(mylnk, delay=4)
    if len(out_put_1) == 0:
        print("看来出了一点问题，他是：")
        print(link_text)
        errorlink_reerror.append(link_text)
    else:
        cnt_allteply += int(out_put_1["reply_num"])
        out_put_2.append(out_put_1)
    b += 1
    llong = len(out_put_2)
    print("当前有效条目：" + str(llong))
    #print("重复查错有效率：%.2f, 预计有效条目数： %d" % ((b/a), before_b+(llong-before_b/b)*a))
    if llong % 17 == 0:
        write_to_json(out_put_2, filename="temp\\temp%03d.json" % llong)
        write_to_json(errorlink_reerror, filename="temp\\errtemp%03d.json" % llong)
print("已完成第3级搜索")
#############################print ("已完成第3级搜索")######################################
out_put_2.append(
    {
        "当前有效条目": len(out_put_2),
        "总回复数": cnt_allteply,
        "总对象数": len(out_put_2) + cnt_allteply
    }
)
write_to_json(out_put_2)
if len(errorlink_reerror) >= 1:
    write_to_json(errorlink_reerror, "errorlink" + str(time.time()) + ".json")
d.close()
